//
//  RoutePlanningCompleteTopView.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/17.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "RoutePlanningCompleteTopView.h"
#import "RoutePlanningCell.h"
#import "RequestLineDetail.h"
#import "UIButton+Dzm.h"
#import "UILabel+Yyx.h"

@interface RoutePlanningCompleteTopView ()



/* 地图上方详情视图 */
@property (weak, nonatomic) IBOutlet UIView *detailView;
/* 头部 */

@property (strong, nonatomic) IBOutlet UILabel *statusLabel;

// 开始时间
@property (weak, nonatomic) IBOutlet UILabel *startTime;
// 起始地点
@property (weak, nonatomic) IBOutlet UILabel *startLocation;
// 结束地点
@property (weak, nonatomic) IBOutlet UILabel *endLocation;
// 价格
@property (weak, nonatomic) IBOutlet UILabel *price;

@property (weak, nonatomic) IBOutlet UIImageView *startImg;

@property (weak, nonatomic) IBOutlet UIImageView *endImg;

// 距离
@property (weak, nonatomic) IBOutlet UILabel *kilometerTime;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *line_height;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *line_1_height;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *line_2_height;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeLine_height;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeLine_2_height;
@property (weak, nonatomic) IBOutlet UIButton *arrowBtn;

@property (weak, nonatomic) IBOutlet UIView *arrowBtnBgView;
@property (weak, nonatomic) IBOutlet UIView *safeBgView;
- (IBAction)secureAction:(UIButton *)sender;
- (IBAction)belateBtnAction:(UIButton *)sender;


@property (assign, nonatomic) BOOL isOpen;



@property (weak, nonatomic) IBOutlet UIView *sectionHeaderView;

@end

@implementation RoutePlanningCompleteTopView

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor clearColor];
     self.statusLabel.hidden = YES;
    [self arrowBtnConfig];
    [self.arrowBtnDelegate arrowBtn:self.isOpen frame:CGRectMake(0, 0, [UIScreen width], 200)];
    [self setExtraCellLineHidden:self.mainTable];
    
    [self configLineConstant];
    
}

-(void)showThroughBusHeaderData:(ResponseLineDetailModel *)responseLineDetailModel{
     self.statusLabel.hidden = NO;
    self.startTime.text     = responseLineDetailModel.SendTime;
    self.startLocation.text = responseLineDetailModel.StartStation;
    self.endLocation.text   = responseLineDetailModel.EndStation;
    
    self.kilometerTime.text = [NSString stringWithFormat:@"%@公里，约%@",self.lineDetailViewModel.responseLineDetailModel.Mileage.stringValue,self.lineDetailViewModel.responseLineDetailModel.UserTime];
    [self.statusLabel statusWaitRegistration];
    
    self.statusLabel.text = responseLineDetailModel.TargetId;
    
    
    NSString *price = responseLineDetailModel.TicketPrice.stringValue;
    if (responseLineDetailModel.IsSale==1) {
        price = responseLineDetailModel.SalePrice.stringValue;
    }
    
    self.price.attributedText = [self getPrice:price leftText:@"有票  " leftSymbol:@"¥"];
}


-(void)showHeaderData:(ResponseLineDetailModel *)responseLineDetailModel{
    
    self.statusLabel.hidden = YES;
    self.startTime.text     = responseLineDetailModel.SendTime;
    
    // 上车点
    [BaseTableViewCell setBusLineStartLocationString:responseLineDetailModel.StartStation startStationType:responseLineDetailModel.StartStationType label:self.startLocation];
    
    self.endLocation.text   = responseLineDetailModel.EndStation;
    self.price.attributedText = [CommoneTools getBusLinePriceString:responseLineDetailModel.TicketPrice.stringValue isSale:responseLineDetailModel.IsSale salePrice:responseLineDetailModel.SalePrice.stringValue];
    
    
    self.kilometerTime.text = [NSString stringWithFormat:@"%@公里，约%@",self.lineDetailViewModel.responseLineDetailModel.Mileage.stringValue,self.lineDetailViewModel.responseLineDetailModel.UserTime];
    
    [self.mainTable reloadData];
}

+(RoutePlanningCompleteTopView *)getRoutePlanningCompleteTopView{
    RoutePlanningCompleteTopView *viewController = [[UIStoryboard storyboardWithName:@"RoutePlanningCompleteTopView" bundle:nil]  instantiateInitialViewController];
    return viewController;
}

-(void)arrowBtnConfig{
    
    CGFloat showHeight = [UIScreen height]  - 64;
    showHeight = self.navigationController.tabBarController.tabBar.hidden?showHeight-12:showHeight - 60;
    
    WEAKSELF
    [self.arrowBtn addActionHandler:^(UIButton *btn) {
        if (weakSelf.isOpen) {
            [weakSelf.detailView layoutIfNeeded];
            [UIView animateWithDuration:0.2 animations:^{
                weakSelf.line_1_height.constant = 150;
             
                [weakSelf.detailView layoutIfNeeded];
            }];
            [weakSelf.arrowBtn setImage:[UIImage imageNamed:@"icon_down"] forState:UIControlStateNormal];
             [self.arrowBtnDelegate arrowBtn:self.isOpen frame:CGRectMake(0, 0, [UIScreen width], 150)];
        }else{
            [weakSelf.detailView layoutIfNeeded];
            [UIView animateWithDuration:0.2 animations:^{
              
                weakSelf.line_1_height.constant = showHeight;
                [weakSelf.detailView layoutIfNeeded];
            }];
            [weakSelf.arrowBtn setImage:[UIImage imageNamed:@"icon_top"] forState:UIControlStateNormal];
            [self.arrowBtnDelegate arrowBtn:self.isOpen frame:CGRectMake(0, 0, [UIScreen width], showHeight)];
        }
        
        weakSelf.isOpen = !weakSelf.isOpen;
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.sectionHeaderView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.lineDetailViewModel) {
        return self.lineDetailViewModel.upDownStations.count;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RoutePlanningCell *routePlanningCell = [tableView dequeueReusableCellWithIdentifier:@"RoutePlanningCell"];
    [routePlanningCell configUpDowmStation:self.lineDetailViewModel.upDownStations[indexPath.row] upStationModel:self.lineDetailViewModel.upStationModel downStationModel:self.lineDetailViewModel.downStationModel];
    return routePlanningCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UpdownstationModel *upDownStationModel = self.lineDetailViewModel.upDownStations[indexPath.row];
    
    
    // 修改上车点
    if (upDownStationModel.Type == 1 || upDownStationModel.Type == 3) {
        self.lineDetailViewModel.upStationModel = upDownStationModel;
        self.lineDetailViewModel.requestLineDetail.StartId = @(upDownStationModel.id).stringValue;
        
    }
    // 修改下车点
    else if (upDownStationModel.Type == 2 || upDownStationModel.Type == 4){
        self.lineDetailViewModel.downStationModel = upDownStationModel;
        self.lineDetailViewModel.requestLineDetail.EndId = @(upDownStationModel.id).stringValue;
    }
    
    
    
    [self.arrowBtnDelegate didSelectRow:upDownStationModel.Lat lng:upDownStationModel.Lng];
}


-(void)configLineConstant{
    self.line_height.constant   = 0.45;
    self.line_2_height.constant = 0.45;
    self.safeLine_height.constant   = 0.45;
    self.safeLine_2_height.constant = 0.45;
}

#pragma mark ------------保险-----------------
- (IBAction)secureAction:(UIButton *)sender {
    
    [self.arrowBtnDelegate secureAction];
}

#pragma mark ------------迟到-----------------
- (IBAction)belateBtnAction:(UIButton *)sender {
     [self.arrowBtnDelegate belateBtnAction];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(NSAttributedString *)getPrice:(NSString *)price leftText:(NSString *)leftText leftSymbol:(NSString *)leftSymbol{
    NSMutableArray *mutableString = [[NSMutableArray alloc ] init];
    [mutableString addObject:[MainInfoTitleFields getInstaceWithText:leftText hexColor:GLOBLE_GRAY_COLOR_5_HEX fontSize:@"10"]];
    [mutableString addObject:[MainInfoTitleFields getInstaceWithText:leftSymbol hexColor:GLOBLE_ORANGE_COLOR_HEX fontSize:@"12"]];
    [mutableString addObject:[MainInfoTitleFields getInstaceWithText:price hexColor:GLOBLE_ORANGE_COLOR_HEX fontSize:@"16"]];
    return [CommoneTools getAttributeString:mutableString];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
